<?php
	class p_tutormodel extends Model {
		function p_tutormodel()
		{
			parent::Model();			
		}
		
		function count_all()
	 	{
	 		$query = $this->db->getwhere('p_tutor', array('is_active'=>1, 'show' => 'Y'));
			$count = $query->num_rows(); 
	  		return $count;
	 	}
	 	
		function getAllLimit($limit = NULL, $offset = NULL)
		{
			$this->db->limit($limit, $offset);			
			$this->db->order_by("seq", "asc"); 
			$query = $this->db->get_where('p_tutor', array('is_active' => 1, 'show' => 'Y'));
			return $query->result();
		}
	 	
		function add($user)
		{
			$data = array(
               'name' => $user['name'] ,
               'detail' => $user['detail'] ,
			   'show' => $user['show'],
			   'seq' => $user['seq'],
			   'create_by' => $user['create_by'],
			   'is_active' => 1
            );
            $this->db->set('create_date', 'NOW()', FALSE); 
			$this->db->insert('p_tutor', $data);
			return $this->db->insert_id();
		}
		
		function updateImagePath($user)
		{			
			$data = array(
               'img_path' => $user['img_path']			   
            );
            $this->db->where('p_tutor_id', $user['p_tutor_id']);
			$this->db->update('p_tutor', $data); 
		}
		
		function update($user)
		{			
			$data = array(
               'name' => $user['name'] ,
               'detail' => $user['detail'] ,
			   'show' => $user['show'],
			   'update_by' => $user['update_by']
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('p_tutor_id', $user['p_tutor_id']);
			$this->db->update('p_tutor', $data); 
		}
		
		function updateSeq($studentID, $seq, $user)
		{			
			$data = array(
               'seq' => $seq,
			   'update_by' => $user
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('p_tutor_id', $studentID);
			$this->db->update('p_tutor', $data); 
		}
		
		function getTutor()
		{
			$this->db->order_by("seq", "asc"); 
			$query = $this->db->get_where('p_tutor', array('show' => 'Y', 'is_active' => 1));
			
			return $query->result();
		}
		
		function getAllTutor()
		{
			$this->db->order_by("seq", "asc"); 
			$query = $this->db->get_where('p_tutor', array('is_active' => 1));
			
			return $query->result();
		}
		
		function getByID($user)
		{
			$query = $this->db->get_where('p_tutor', array('p_tutor_id' => $user, 'is_active' => 1));
			return $query->row_array();
		}
		
		function getMaxSeq()
		{
			$this->db->select_max('seq');
			$query = $this->db->get('p_tutor');
			$data = $query->row_array();
			if($data['seq'] == null)
				return 1;
				
			return $data['seq']+1;
		}
		
		function delete($id)
		{
			$data = array(
               'is_active' => 0,
			   'update_by' => $this->session->userdata('username')   
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('p_tutor_id', $id);
			$this->db->update('p_tutor', $data); 
		}
	}
?>